﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Modulo5
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Count() != 4)
            {
                Console.WriteLine("Args error!");
                return;
            }
            var dataEntreFileName = args[0];
            var dataOutputFileName = args[1];
            var logFileName = args[2];
            var errFileName = args[3];

            int entryRegisterCout = 0;
            int procesedResgisterCount = 0;
            int outputRegisterCount = 0;

            var outputFile = new StreamWriter(dataOutputFileName);
            var logFile = new StreamWriter(logFileName);
            var errFile = new StreamWriter(errFileName);

            var dictionary = new Dictionary<string, int>();

            logFile.WriteLine("==== Module #5====");
            logFile.WriteLine(DateTime.Now.ToString());
            errFile.WriteLine("==== Module #5====");
            errFile.WriteLine(DateTime.Now.ToString());

            const char Separator = '\n';
            var words = File.ReadAllText(dataEntreFileName, Encoding.UTF8).Split(new[] { Separator });
            var lastWord = words.First().Trim(Separator);
            entryRegisterCout++;

            for (int i = 0; i < words.Count(); i++ )
            {
                string currentWord = words.ElementAt(i).Trim(Separator);

                if( currentWord.Length == 0 )
                {
                    continue;
                }

                entryRegisterCout++;

                string bigram = "<" + lastWord + " , " + currentWord + ">";
                if( IsValidBigram( lastWord, currentWord ) )
                {
                    if( dictionary.ContainsKey(bigram))
                    {
                        int ocurrence = dictionary[bigram];
                        dictionary[bigram] = 1 + ocurrence;
                    }else
                    {
                        dictionary.Add(bigram, 1);
                    }
                }else
                {
                    errFile.WriteLine(bigram + " is not valid.");
                }
                procesedResgisterCount++;
                lastWord = currentWord;
            }

            foreach (var bigram in dictionary.Keys)
            {
                outputFile.WriteLine(bigram + " : " + dictionary[bigram]);
            }
            outputRegisterCount = dictionary.Keys.Count;

            logFile.WriteLine("Input Register Count : " + entryRegisterCout);
            logFile.WriteLine("Procesed Register Count : " + procesedResgisterCount);
            logFile.WriteLine("Output Register Count : " + outputRegisterCount);
            logFile.WriteLine(DateTime.Now.ToString());
            errFile.WriteLine(DateTime.Now.ToString());

            outputFile.Flush();
            logFile.Flush();
            errFile.Flush();

            outputFile.Close();
            logFile.Close();
            errFile.Close();
        }

        private static bool IsValidBigram(string firstWord, string secondWord)
        {
            var stringWord = new String(firstWord.Where(Char.IsLetter).ToArray());
            var separator = firstWord.Trim(stringWord.ToArray());

            var firstType = firstWord.Split(separator.ToArray()).First();
            var secondType = secondWord.Split(separator.ToArray()).First();
            if( !firstType.Equals(secondType))
            {
                return true;
            }
            return false;
        }
    }
}
